<?php
namespace Home\Controller;
use Think\Controller;
defined('IN_ECTOUCH') or die('Deny Access');
class SmsController extends Controller {

    protected $mobile;
    protected $mobile_code;
    //短信验证码
    public function __construct() {
        //测试
        // $_POST['mobile_phone']='13758070546';
        // $_POST['flag']='forget';


        $this->mobile =I('post.mobile_phone');
        

        // $this->mobile_code =I('post.mobile_code');
        // $this->sms_code =I('post.sms_code');
    }
    //发送
    public function send() {

        if (empty($this->mobile)) {
            return_data(20001,'手机号码不能为空');
        }

        $preg = '/^1[0-9]{10}$/'; //简单的方法
        if (!preg_match($preg, $this->mobile)) {
            return_data(20002,'手机号码格式不正确');
        }

        if ($this->mobile) {
            $path= '/Data/smslog/' . date('Ymd') . '/';
            $file_name=$this->mobile. '.log';
            $lasttime=strtotime(read_file($path,$file_name));

            if ( $lasttime> (time() - 60)) {
                return_data(20004,'操作次数太多');
            }
        }
        
        $Users=M('Users');
        $where['mobile_phone'] = $this->mobile;
        $user_id = $Users->where($where)->getField('user_id');

        if ($_POST['flag'] == 'register') {
            //手机注册
            if (!empty($user_id)) {
                return_data(40001,'手机号码已存在');
            }
        } elseif ($_POST['flag'] == 'forget') {
            //找回密码
            if (empty($user_id)) {
               return_data(40004,'手机号码不存在');
            }
        }
        else
        {
          return_data(10001,'参数不完整');
        }



        $this->mobile_code = random(6, 1);
        $message = "【锦绣天地】您的短信验证码是". $this->mobile_code ;
        $sms_error = '';
        $mobile=$this->mobile;
        $target = "http://xtx.telhk.cn:8080/sms.aspx?action=send";
        $post_data =  "userid=6319&account=mysilk&password=jx908877&mobile={$mobile}&content={$message}&sendTime=&extno=";

         $gets =  $this->xml_to_array(post_data($post_data, $target));

        $path= '/Data/smslog/' . date('Ymd') . '/';
        $file_name=$this->mobile. '.log';

         write_file($path,$file_name, date("Y-m-d H:i:s"));

        // $gets['returnsms']['returnstatus']=='Success'
        if ($gets['returnsms']['returnstatus']=='Success') {
            $data['sms_mobile_code']= $this->mobile_code;

              $sms_data=array();
              $sms_data['mobile_phone']=$mobile;
              $sms_data['mobile_code']=$this->mobile_code;
              if($_POST['flag'] == 'register') $sms_data['notes']='注册或修改手机';
              else  if($_POST['flag'] == 'forget') $sms_data['notes']='找回密码';

              if($user_id)$sms_data['user_id']=$user_id;
              $sms_data['ctime']=gmtime();
              M("sms_mobile")->add($sms_data);
            
            return_data(0,'验证码发送成功',$data['sms_mobile_code']);
            // exit(json_encode(array('code' => 2)));
        } else {
           return_data(40002,'手机短信验证码发送失败');
        }
    }


       protected function xml_to_array($xml){
          $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
          if(preg_match_all($reg, $xml, $matches)){
              $count = count($matches[0]);
              for($i = 0; $i < $count; $i++){
                  $subxml= $matches[2][$i];
                  $key = $matches[1][$i];
                  if(preg_match( $reg, $subxml )){
                      $arr[$key] = $this->xml_to_array( $subxml );
                  }else{
                      $arr[$key] = $subxml;
                  }
              }
        }
        return $arr;
      }


   

}